package com.nanako.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanako.dao.CategoryMapper;
import com.nanako.entity.Category;
import com.nanako.entity.Dish;
import com.nanako.entity.Setmeal;
import com.nanako.exception.CascadingDeleteException;
import com.nanako.service.CategoryService;
import com.nanako.service.DishService;
import com.nanako.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    //@Lazy
    @Autowired
    private SetmealService setmealService;


    @Transactional
    public boolean cascadingDeleteCheck(Long id) {
        // 判断当前分类下是否有菜品，有菜品则不能删除
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
        if (dishService.count(dishWrapper) > 0) {
            throw new CascadingDeleteException("当前分类下有菜品，不能删除");
        }
        // 判断当前分类下是否有套餐，有套餐则不能删除
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId, id);
        if (setmealService.count(setmealWrapper) > 0) {
            throw new CascadingDeleteException("当前分类下有套餐，不能删除");
        }
        return true;
    }
}
